// pages/clothSearch/clothSearch.js
var util = require("../../utils/util");
var clothUtil = require("../../utils/clothUtil.js");
Page({
  data:{
    loadHide: true,
    hideSearchMask: true, // 是否隐藏搜索遮罩
    isEmpty: false,
    searchType: 0, // 0-所有 1-成衣 2-面料 3-成衣扫码 4-面料扫码
    categoryStr: '成衣',
    productList: [],
    keyword: '',
    pageNo : 0,  // 当前分页
    pageSize : 20, // 单页数量
    totalPage : 0, // 总页数
    totalNum : 0 // 总数量
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    var searchType = options.searchType || 0;
    var keyword = options.keyword || '';
    var categoryStr = (searchType == 2 || searchType == 4) ? "面料" : "成衣";
    this.setData({
      keyword: keyword,
      searchType: searchType,
      categoryStr: categoryStr
    });
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight:res.windowHeight
        });
      }
    });
    clothUtil.search(that, 1);
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  scroll: function(e){
    // 滚动事件
    this.setData({
      scrollTop: e.detail.scrollTop
    });
  },
  searchCloth: function(e){
    // 搜索
    var keyword = e.detail.value;
    if(keyword.replace(/(^s*)|(s*$)/g, "").length == 0) return;
    this.setData({
      keyword: keyword,
      searchType: this.data.categoryStr == '成衣' ? 1 : 2,
      loadHide: false,
      isEmpty: false
    });
    clothUtil.search(this, 1);
  },
  gotoDetail: function(e){
      var productId = e.currentTarget.dataset.productid;
      var productName = e.currentTarget.dataset.productname;
      var url = "";
      if(this.data.categoryStr == '成衣'){
        url = '../clothDetail/clothDetail?productId='+productId+'&productName='+productName;
      }else{
        url = '../fabricDetail/fabricDetail?productId='+productId+'&productName='+productName;
      }
      wx.navigateTo({
          url: url
      })
  },
  bindDownload: function(){
    // 该方法绑定页面滑动到底部事件
    console.log("download");
    this.setData({
      loadHide:false
    });
    clothUtil.search(this, 0);
  },
  searchClothTxt: function(){},
  onFocusSearch: function(){
    this.setData({hideSearchMask: false});
  },
  onBlurSearch: function(){this.setData({hideSearchMask: true});},
  onScanCode: function(){
    var that = this;
    wx.scanCode({
      success: function(res){
        var keyword = util.trim(res.result);
        if(keyword == "") return;
        that.setData({
          keyword: keyword,
          searchType: that.data.categoryStr == '成衣' ? 3 : 4,
          loadHide: false
        });
        clothUtil.search(that, 1);
      },
      fail: function() {
        // fail
        wx.showModal({
          title: '提示',
          content: '识别失败，请确认后重新扫描',
          showCancel: false,
          confirmText: '确认',
          confirmColor: '#111111',
          success: function(res){
          },
          fail: function(){}
        });
      },
      complete: function() {
        // complete
      }
    })
  }
})